'use strict';
/**
 * Created by FreezeSoul on 2015/9/18.
 */
define(['angular',
        'jquery',
        'app/core/util',
        'ocLazyLoad',
        'angularUiRouter',
        'angularAnimate',
        'ocLazyLoadUiRouterDecorator'
    ],
    function (angular, $, Util) {
        angular.module('ad.appWeb', [
            'ui.router',
            'oc.lazyLoad',
            'ngAnimate',
            'oc.lazyLoad.uiRouterDecorator'
        ])
            .factory('adRequest', ['$http', '$rootScope', '$state', function ($http, $rootScope, $state) {
                return function (url, args) {
                    var config = {
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
                        }
                    };
                    if ($rootScope.currentUser) {
                        url = url + "?" + $.param({
                            ticket: (new Date()).getTime()
                        });
                        config = {
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;',
                                'Authorization': 'Bearer ' + $rootScope.currentUser.UserToken
                            }
                        };
                    }
                    return $http.post(url, $.param(args === undefined ? {} : args), config);
                };
            }])
            .factory('httpErrorsInterceptor', ['$q', '$rootScope', '$injector', function ($q, $rootScope, $injector) {
                return {
                    'request': function (config) {
                        return config;
                    },
                    'response': function (response) {
                        if (response.status === 401) {
                            $injector.get('$state').go('login');
                            return $q.reject(response);
                        }
                        return response;
                    },
                    'responseError': function (rejection) {
                        $rootScope.$broadcast('ad.appWeb.http.error', rejection);
                        return $q.reject(rejection);
                    }
                };
            }])
            .config(['$stateProvider', '$urlRouterProvider', '$ocLazyLoadProvider', '$httpProvider', '$animateProvider', '$locationProvider',
                function ($stateProvider, $urlRouterProvider, $ocLazyLoadProvider, $httpProvider, $animateProvider, $locationProvider) {
                    $locationProvider.html5Mode(false);
                    $animateProvider.classNameFilter(/^(?:(?!ng-animate-disabled).)*$/);
                    $httpProvider.interceptors.push('httpErrorsInterceptor');
                    $ocLazyLoadProvider.config({
                        debug: false,
                        events: false,
                        loadedModules: ['ad.appWeb'],
                        asyncLoader: require
                    });

                    $urlRouterProvider.otherwise('/login');

                    $stateProvider
                        .state('base', {
                            abstract: true,
                            url: '',
                            templateUrl: 'app/views/base.tpl.html'
                        })

                        //login
                        .state('login', {
                            url: '/login',
                            parent: 'base',
                            controller: 'adLoginController',
                            lazyModule: 'ad.login',
                            lazyFiles: [
                                'app/modules/login/define',
                                'app/modules/login/controllers/login'
                            ],
                            lazyTemplateUrl: 'app/modules/login/views/login.tpl.html'
                        })

                        //include
                        .state('include', {
                            url: '/dashboard/include/{pageid}',
                            parent: 'base',
                            controller: 'adIncludeController',
                            lazyModule: 'ad.include',
                            lazyFiles: [
                                'app/modules/include/define',
                                'app/modules/include/controllers/include',
                                'app/modules/include/directives/dashboard'
                            ],
                            lazyTemplateUrl: 'app/modules/include/views/include.tpl.html'
                        })

                        //player
                        .state('player', {
                            url: '/dashboard/player',
                            parent: 'base',
                            controller: 'adPlayerController',
                            lazyModule: 'ad.player',
                            lazyFiles: [
                                'app/modules/player/define',
                                'app/modules/player/controllers/player',
                                'app/modules/player/directives/header',
                                'app/modules/player/directives/sidebar',
                                'app/modules/player/directives/dashboard'
                            ],
                            lazyTemplateUrl: 'app/modules/player/views/player.tpl.html'
                        })

                        //editor
                        .state('editor', {
                            url: '/dashboard/editor',
                            parent: 'base',
                            controller: 'adEditorController',
                            lazyModule: 'ad.editor',
                            lazyFiles: [
                                'app/modules/editor/define',
                                'app/modules/editor/controllers/editor',
                                'app/modules/editor/directives/header',
                                'app/modules/editor/directives/sidebar'
                            ],
                            lazyTemplateUrl: 'app/modules/editor/views/editor.tpl.html'
                        })
                        .state('editor.home', {
                            url: '/home',
                            parent: 'editor',
                            templateUrl: 'app/views/home.tpl.html'
                        })
                        .state('editor.dictionary', {
                            url: '/dictionary',
                            parent: 'editor',
                            controller: 'adDictionaryController',
                            lazyModule: 'ad.dictionary',
                            lazyFiles: [
                                'app/modules/dictionary/define',
                                'app/modules/dictionary/controllers/dictionary-list'
                            ],
                            lazyTemplateUrl: 'app/modules/dictionary/views/dictionary-list.tpl.html'
                        })
                        .state('editor.user', {
                            url: '/user',
                            parent: 'editor',
                            controller: 'adUsersController',
                            lazyModule: 'ad.users',
                            lazyFiles: [
                                'app/modules/users/define',
                                'app/modules/users/controllers/users'
                            ],
                            lazyTemplateUrl: 'app/modules/users/views/users-list.tpl.html'
                        })
                        .state('editor.role', {
                            url: '/role',
                            parent: 'editor',
                            controller: 'adRolesController',
                            lazyModule: 'ad.roles',
                            lazyFiles: [
                                'app/modules/folder/define',
                                'app/modules/index/define',
                                'app/modules/page/define',
                                'app/modules/roles/define',
                                'app/modules/roles/controllers/roles'
                            ],
                            lazyTemplateUrl: 'app/modules/roles/views/roles.tpl.html'
                        })
                        .state('editor.import', {
                            url: '/import',
                            parent: 'editor',
                            controller: 'adImportController',
                            lazyModule: 'ad.import',
                            lazyFiles: [
                                'app/modules/import/define',
                                'app/modules/import/controllers/import'
                            ],
                            lazyTemplateUrl: 'app/modules/import/views/import.tpl.html'
                        })
                        .state('editor.schema', {
                            url: '/schema',
                            parent: 'editor',
                            controller: 'adSchemaController',
                            lazyModule: 'ad.schema',
                            lazyFiles: [
                                'app/modules/folder/define',
                                'app/modules/dataset/define',
                                'app/modules/schema/define',
                                'app/modules/schema/controllers/schema'
                            ],
                            lazyTemplateUrl: 'app/modules/schema/views/schema.tpl.html'
                        })
                        .state('editor.schema.default', {
                            url: '/default',
                            parent: 'editor.schema',
                            controller: 'adSchemaSearchController',
                            lazyModule: 'ad.schema',
                            lazyFiles: [
                                'app/modules/schema/define',
                                'app/modules/schema/controllers/schema-search'
                            ],
                            lazyTemplateUrl: 'app/modules/schema/views/schema-search.tpl.html'
                        })
                        .state('editor.schema.schema-edit', {
                            url: '/schema-edit/{schemaid:[0-9]{1,8}}',
                            parent: 'editor.schema',
                            controller: 'adSchemaEditController',
                            lazyModule: 'ad.schema',
                            lazyFiles: [
                                'app/modules/schema/define',
                                'app/modules/schema/controllers/schema-edit'
                            ],
                            lazyTemplateUrl: 'app/modules/schema/views/schema-edit.tpl.html'
                        })
                        .state('editor.source', {
                            url: '/source',
                            parent: 'editor',
                            controller: 'adSourceController',
                            lazyModule: 'ad.source',
                            lazyFiles: [
                                'app/modules/source/define',
                                'app/modules/source/controllers/source'
                            ],
                            lazyTemplateUrl: 'app/modules/source/views/source.tpl.html'
                        })
                        .state('editor.schema.dataset', {
                            url: '/dataset',
                            parent: 'editor.schema',
                            controller: 'adDataSetSearchController',
                            lazyModule: 'ad.dataset',
                            lazyFiles: [
                                'app/modules/dataset/define',
                                'app/modules/dataset/controllers/dataset'
                            ],
                            lazyTemplateUrl: 'app/modules/dataset/views/dataset-search.tpl.html'
                        })
                        .state('editor.schema.dataset-edit', {
                            url: '/dataset-edit/{schemaid:[0-9]{1,8}}/{datasetid:[0-9]{1,8}}',
                            parent: 'editor.schema',
                            controller: 'adDataSetEditController',
                            lazyModule: 'ad.dataset',
                            lazyFiles: [
                                'app/modules/dataset/define',
                                'app/modules/dataset/controllers/dataset-edit'
                            ],
                            lazyTemplateUrl: 'app/modules/dataset/views/dataset-edit.tpl.html'
                        })
                        .state('editor.schema.dataset-edit.database', {
                            url: '/database',
                            parent: 'editor.schema.dataset-edit',
                            controller: 'adDataSetDataBaseController',
                            lazyModule: 'ad.dataset',
                            lazyFiles: [
                                'app/modules/dataset/define',
                                'app/modules/dataset/controllers/dataset-datasource-database'
                            ],
                            lazyTemplateUrl: 'app/modules/dataset/views/dataset-datasource-database.tpl.html'
                        })
                        .state('editor.schema.dataset-edit.excel', {
                            url: '/excel',
                            parent: 'editor.schema.dataset-edit',
                            controller: 'adDataSetExcelController',
                            lazyModule: 'ad.dataset',
                            lazyFiles: [
                                'app/modules/dataset/define',
                                'app/modules/dataset/controllers/dataset-datasource-excel'
                            ],
                            lazyTemplateUrl: 'app/modules/dataset/views/dataset-datasource-excel.tpl.html'
                        })
                        .state('editor.schema.dataset-edit.service', {
                            url: '/service',
                            parent: 'editor.schema.dataset-edit',
                            controller: 'adDataSetServiceController',
                            lazyModule: 'ad.dataset',
                            lazyFiles: [
                                'app/modules/dataset/define',
                                'app/modules/dataset/controllers/dataset-datasource-service'
                            ],
                            lazyTemplateUrl: 'app/modules/dataset/views/dataset-datasource-service.tpl.html'
                        })
                        .state('editor.index', {
                            url: '/index',
                            parent: 'editor',
                            controller: 'adIndexController',
                            lazyModule: 'ad.index',
                            lazyFiles: [
                                'app/modules/folder/define',
                                'app/modules/index/define',
                                'app/modules/index/controllers/index'
                            ],
                            lazyTemplateUrl: 'app/modules/index/views/index.tpl.html'
                        })
                        .state('editor.index.search', {
                            url: '/search',
                            parent: 'editor.index',
                            controller: 'adIndexSearchController',
                            lazyModule: 'ad.index',
                            lazyFiles: [
                                'app/modules/index/define',
                                'app/modules/index/controllers/index-search'
                            ],
                            lazyTemplateUrl: 'app/modules/index/views/index-search.tpl.html'
                        })
                        .state('editor.index-edit', {
                            url: '/index-edit/{chartid:[0-9]{1,8}}',
                            parent: 'editor',
                            controller: 'adIndexEditController',
                            lazyModule: 'ad.index',
                            lazyFiles: [
                                'app/core/editor',
                                'app/modules/folder/define',
                                'app/modules/dataset/define',
                                'app/modules/schema/define',
                                'app/modules/page/define',
                                'app/modules/index/define',
                                'app/modules/index/controllers/index-edit'
                            ],
                            lazyTemplateUrl: 'app/modules/index/views/index-edit.tpl.html'
                        })
                        .state('editor.page', {
                            url: '/page',
                            parent: 'editor',
                            controller: 'adPageController',
                            lazyModule: 'ad.page',
                            lazyFiles: [
                                'app/modules/folder/define',
                                'app/modules/page/define',
                                'app/modules/page/controllers/page'
                            ],
                            lazyTemplateUrl: 'app/modules/page/views/page.tpl.html'
                        })
                        .state('editor.page.search', {
                            url: '/search',
                            parent: 'editor.page',
                            controller: 'adPageSearchController',
                            lazyModule: 'ad.page',
                            lazyFiles: [
                                'app/modules/page/define',
                                'app/modules/page/controllers/page-search'
                            ],
                            lazyTemplateUrl: 'app/modules/page/views/page-search.tpl.html'
                        })
                        .state('editor.page-edit', {
                            url: '/page-edit/{pageid:[0-9]{1,8}}',
                            parent: 'editor',
                            controller: 'adPageEditController',
                            lazyModule: 'ad.page',
                            lazyFiles: [
                                'app/modules/folder/define',
                                'app/modules/index/define',
                                'app/modules/page/define',
                                'app/modules/page/controllers/page-edit'
                            ],
                            lazyTemplateUrl: 'app/modules/page/views/page-edit.tpl.html'
                        })
                        .state('editor.setting', {
                            url: '/setting',
                            parent: 'editor',
                            templateUrl: 'app/views/setting.tpl.html'
                        });
                }
            ]).run(['$rootScope', '$state', function ($rootScope, $state) {
            $rootScope.$on('$stateChangeStart', function (e, to) {
                if ($rootScope.currentUser == null) {
                    $rootScope.currentUser = window.sessionStorage ? angular.fromJson(window.sessionStorage.getItem('amazing-dashboard-user')) : null;
                }
                if (to.name.indexOf('include') > -1) {
                    var token = Util.getUrlParameter('token');
                    if (token && !$rootScope.currentUser) {
                        $rootScope.currentUser = {UserToken: token};
                        window.__token = token;
                    }
                }
                if (to.name !== 'login') {
                    if (!$rootScope.currentUser) {
                        e.preventDefault();
                        $state.go('login');
                    } else {
                        if (to.name.indexOf('editor') > -1 &&
                            !$rootScope.currentUser.IsConfigEditor) {
                            e.preventDefault();
                            $state.go('login');
                        }
                    }
                }
            });

        }]);
    });